package com.ljx.blog.article.controller;

import com.ljx.blog.article.domain.entity.Article;
import com.ljx.blog.article.domain.vo.req.ArticlePageReq;
import com.ljx.blog.article.domain.vo.req.ArticleSubmitCheckReq;
import com.ljx.blog.article.domain.vo.req.ArticleSubmitReq;
import com.ljx.blog.article.domain.vo.resp.ArticleResp;
import com.ljx.blog.article.service.ArticleService;
import com.ljx.blog.common.domain.vo.resp.ApiResult;
import com.ljx.blog.common.domain.vo.resp.PageResp;
import com.ljx.blog.security.annotation.ApiPermission;
import com.ljx.blog.security.common.PermissionConstant;
import com.ljx.blog.security.util.UserUtil;
import com.ljx.blog.user.domain.entity.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-12  21:22
 */
@Api(tags = "文章接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation("获取文章信息")
    @GetMapping("/info/{id}")
    public ApiResult<ArticleResp> getInfo(@PathVariable("id") Long articleId){
        return ApiResult.success(articleService.getInfo(articleId));
    }


    @ApiOperation("获取租户文章分页")
    @GetMapping("/page")
    public ApiResult<PageResp<ArticleResp>> getPage(ArticlePageReq req){
        return ApiResult.success(articleService.getPage(req));
    }

    @ApiOperation("获取租户用户文章分页")
    @GetMapping("/user/page")
    public ApiResult<PageResp<ArticleResp>> getUserArticlePage(ArticlePageReq req){
        req.setUserId(UserUtil.getUserId());
        return ApiResult.success(articleService.getPage(req));
    }


    @ApiPermission(permissions = {PermissionConstant.ARTICLE_NEW_EDIT})
    @ApiOperation("保存/更新文章")
    @PostMapping("/submit")
    public ApiResult<ArticleResp> submit(@Validated @RequestBody ArticleSubmitReq req){
        return ApiResult.success(articleService.submit(req));
    }

    @ApiPermission(permissions = {PermissionConstant.ARTICLE_NEW_EDIT})
    @ApiOperation("保存/更新文章前检查资格")
    @PostMapping("/submit/check")
    public ApiResult<Boolean> submitCheck(@Validated @RequestBody ArticleSubmitCheckReq req){
        return ApiResult.success(articleService.submitCheck(req));
    }


    @ApiPermission(permissions = {PermissionConstant.ARTICLE_LIST, PermissionConstant.ADMIN_LIST})
    @ApiOperation("获取租户文章分页（管理员）")
    @GetMapping("/admin/page")
    public ApiResult<PageResp<Article>> getPageByAdmin(ArticlePageReq req){
        return ApiResult.success(articleService.getPageByAdmin(req));
    }


}
